using System;
using System.Collections;
using Whitemice.MOGI;

namespace Whitemice.MOGI.ZOGI 
{	
	public class Task : Whitemice.MOGI.ZOGI.Document, Whitemice.MOGI.ITask
	{
		public Task(IServer server, Payload payload) : base(server, payload)
		{
		}
		
		/// \brief Task end date
		public DateTime EndDate 
		{ 
			get { return payload.GetDateTimeForKey("end"); }
			set { payload.SetValueForKey("end", value); }
		}
		
		/// \brief Task start date
		public DateTime StartDate 
		{ 
			get { return payload.GetDateTimeForKey("start"); }
			set { payload.SetValueForKey("start", value); }
		}
		
		/// \brief Task start date
		public DateTime CompletionDate 
		{ 
			get { return payload.GetDateTimeForKey("completionDate"); }
			set 
			{
				if (value == DateTime.MinValue)
					payload.SetValueForKey("completionDate", null);
				else
					payload.SetValueForKey("completionDate", value); 
			}
		}
		
		/// \brief Document id of task executor
		public string Executor 
		{ 
			get 
			{ 
				return iserver.GetName(payload.GetIntForKey("executantObjectId").ToString());
			}
			set 
			{ 
				payload.SetValueForKey("executantObjectId", MOGI.Id(value));
			}
		}
		
		/// \brief Document id of task creator
		public string Creator 
		{ 
			get 
			{ 
				return iserver.GetName(payload.GetIntForKey("creatorObjectId").ToString()); 
			}
			set { }
		}
		/// \brief Task status
		public ETaskStatus Status 
		{ 
			get 
			{
		  	switch (payload.GetStringForKey("status")) 
		  	{
				case "00_created": return ETaskStatus.Created;
				case "02_rejected": return ETaskStatus.Rejected;
				case "05_accepted": return ETaskStatus.Accepted;
				case "10_archived": return ETaskStatus.Archived;
				case "10_commented": return ETaskStatus.Commented;
				case "15_divided": return ETaskStatus.Divided;
				case "20_processing": return ETaskStatus.Processing;
				case "25_done": return ETaskStatus.Done;
				case "27_reactivated": return ETaskStatus.Reactivated;
				case "30_archived": return ETaskStatus.Archived;
				default: return ETaskStatus.Created;
		  	}					
			}
		}
		
		/// \brief Task Keywords
		public string Keywords 
		{ 
			get { return payload.GetStringForKey("keywords"); }
			set { payload.SetValueForKey("keywords", value); }
		}
		
		/// \brief Task description or name
		public string Description 
		{ 
			get { return payload.GetStringForKey("name"); }
			set { payload.SetValueForKey("name", value); }
		}
		
		/// \brief Task priorit
		public ETaskPriority Priority 
		{ 
			get
			{
				switch (payload.GetIntForKey("priority"))
				{
					case 1: return ETaskPriority.VeryHigh;
					case 2: return ETaskPriority.High;
					case 4: return ETaskPriority.Low;
					case 5: return ETaskPriority.VeryLow;
					default: return ETaskPriority.Average;
				}
			}
			set { payload.SetValueForKey("priority", (int)value); }
		}
		
		/// \brief Task percentage complete
		public int PercentComplete 
		{ 
			get { return payload.GetIntForKey("percentComplete"); }
			set { payload.SetValueForKey("percentComplete", value); }
		}
		
		public string Info 
		{ 
			get { return payload.GetStringForKey("accountingInfo"); }
			set { payload.SetValueForKey("accountingInfo", value); }
		}
		
		public string Companies 
		{ 
			get { return payload.GetStringForKey("associatedCompanies"); }
			set { payload.SetValueForKey("associatedCompanies", value); }
		}
		
		public string Contacts 
		{ 
			get { return payload.GetStringForKey("associatedContacts"); }
			set { payload.SetValueForKey("associatedContacts", value); }
		}
		
		public int ActualWork 
		{ 
			get { return payload.GetIntForKey("actualWork"); }
			set { payload.SetValueForKey("actualWork", value); }
		}
		
		public int TotalWork 
		{ 
			get { return payload.GetIntForKey("totalWork"); }
			set { payload.SetValueForKey("totalWork", value); }
		}
		
		public int Kilometers 
		{ 
			get { return payload.GetIntForKey("kilometers"); }
			set { payload.SetValueForKey("kilometers", value); }
		}
		
		public int Miles 
		{ 
			get 
			{ 
				return Convert.ToInt32((payload.GetIntForKey("kilometers") * 0.6));
			}
			set 
			{
				payload.SetValueForKey("kilometers",
					Convert.ToInt32(Convert.ToDouble(value) * 1.2)); 
			}
		}
		
		public ETaskSensitivity Sensitivity 
		{ 
			get 
			{ 
				switch (payload.GetIntForKey("sensitivity"))
				{
					case 0 : return ETaskSensitivity.Public;
					case 1 : return ETaskSensitivity.Personal;
					case 2 : return ETaskSensitivity.Private;
					case 3 : return ETaskSensitivity.Confidential;
					default: return ETaskSensitivity.Undefined;
				}			
			}
			set { payload.SetValueForKey("sensitivity", (int)value); }
		}
		
		public string Category 
		{ 
			get { return payload.GetStringForKey("category"); }
			set { payload.SetValueForKey("category", value); }
		}
	}
}
